package com.apartment.own.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.apartment.common.annotation.Excel;
import com.apartment.own.domain.OwnResArticleInventory;
import com.apartment.own.request.res.ResArticleTemplateDTO;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.apartment.common.annotation.Log;
import com.apartment.common.core.controller.BaseController;
import com.apartment.common.core.domain.AjaxResult;
import com.apartment.common.enums.BusinessType;
import com.apartment.own.domain.OwnResArticleTemplate;
import com.apartment.own.service.IOwnResArticleTemplateService;
import com.apartment.common.utils.poi.ExcelUtil;
import com.apartment.common.core.page.TableDataInfo;

/**
 * 物品模板Controller
 *
 * @author @Dog_E
 * @date 2020-08-28
 */
@RestController
@RequestMapping("/own/template")
@Api(value = "物品模板", tags = "物品模板")
public class OwnResArticleTemplateController extends BaseController {
    @Autowired
    private IOwnResArticleTemplateService ownResArticleTemplateService;

    /**
     * 查询物品模板列表
     */
    @PreAuthorize("@ss.hasPermi('own:template:list')")
    @GetMapping("/list")
    @ApiOperation("查询物品模板列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "templateType", value = "租房类型 ", required = true),
    })
    public TableDataInfo list(OwnResArticleTemplate ownResArticleTemplate) {
        startPage();
        List<OwnResArticleTemplate> list = ownResArticleTemplateService.selectOwnResArticleTemplateList(ownResArticleTemplate);
        return getDataTable(list);
    }

    /**
     * 导出物品模板列表
     */
    @PreAuthorize("@ss.hasPermi('own:template:export')")
    @Log(title = "物品模板", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(OwnResArticleTemplate ownResArticleTemplate) {
        List<OwnResArticleTemplate> list = ownResArticleTemplateService.selectOwnResArticleTemplateList(ownResArticleTemplate);
        ExcelUtil<OwnResArticleTemplate> util = new ExcelUtil<OwnResArticleTemplate>(OwnResArticleTemplate.class);
        return util.exportExcel(list, "template");
    }

    /**
     * 获取物品模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('own:template:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(ownResArticleTemplateService.selectOwnResArticleTemplateById(id));
    }

//    /**
//     * 新增物品模板
//     */
//    @PreAuthorize("@ss.hasPermi('own:template:add')")
//    @Log(title = "物品模板", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody OwnResArticleTemplate ownResArticleTemplate)
//    {
//        return toAjax(ownResArticleTemplateService.insertOwnResArticleTemplate(ownResArticleTemplate));
//    }

//    /**
//     * 修改物品模板
//     */
//    @PreAuthorize("@ss.hasPermi('own:template:edit')")
//    @Log(title = "物品模板", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody OwnResArticleTemplate ownResArticleTemplate) {
//        return toAjax(ownResArticleTemplateService.updateOwnResArticleTemplate(ownResArticleTemplate));
//    }

    /**
     * 删除物品模板
     */
    @PreAuthorize("@ss.hasPermi('own:template:remove')")
    @Log(title = "物品模板", businessType = BusinessType.DELETE)
    @ApiOperation("删除物品模板与物品")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ownResArticleTemplateService.deleteOwnResArticleTemplateByIds(ids));
    }


    /**
     * @param templateDTO: 物品模板与物品 类
     * @Description: 新增物品模板与物品
     * @return: com.apartment.common.core.domain.AjaxResult
     * @Author: @Dog_E
     * @Date: 2020/9/8 0008 13:53
     **/
    @PreAuthorize("@ss.hasPermi('own:template:add')")
    @Log(title = "物品模板", businessType = BusinessType.INSERT)
    @ApiOperation("新增物品模板与物品")
    @PostMapping()
    public AjaxResult add(@RequestBody ResArticleTemplateDTO templateDTO) {
        return toAjax(ownResArticleTemplateService.addTemplateAndArticle(templateDTO));
    }

    /**
     * @param id: 模板id
     * @Description: 查询物品模板对应的物品
     * @return: com.apartment.common.core.domain.AjaxResult
     * @Author: @Dog_E
     * @Date: 2020/9/8 0008 15:33
     **/
    @PreAuthorize("@ss.hasPermi('own:template:list')")
    @GetMapping("articleList/{id}")
    @ApiOperation("查询物品模板对应的物品")
    @ApiImplicitParam(name = "id", value = "模板id 模板与物品详情关系 一对多", required = true)
    public AjaxResult articleList(@PathVariable String id) {
        return AjaxResult.success(ownResArticleTemplateService.articleList(id));
    }


    /**
     * @param templateDTO: 物品模板与物品 类
     * @Description: 修改物品模板与物品
     * @return: com.apartment.common.core.domain.AjaxResult
     * @Author: @Dog_E
     * @Date: 2020/9/8 0008 18:26
     **/
    @PreAuthorize("@ss.hasPermi('own:template:edit')")
    @Log(title = "物品模板", businessType = BusinessType.UPDATE)
    @ApiOperation("修改物品模板与物品")
    @PutMapping
    public AjaxResult edit(@RequestBody ResArticleTemplateDTO templateDTO) {
        return toAjax(ownResArticleTemplateService.updateTemplateAndArticle(templateDTO));
    }


}
